<template>
  <h1 style="margin-bottom: 30px ;">员工职位统计</h1>
  <div class="chart-wrapper">
    <div class="chart-container1">
      <!-- ECharts 容器 -->
      <div class="chart" id="container1" />
    </div>

    <div class="chart-container1">
      <!-- ECharts 容器 -->
      <div class="chart" id="container2" />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'

import { selectEmpJobDataApi } from '../../../api/report/empJobData'
import { selectEmpGenderDataApi } from '../../../api/report/empGenderData'
import * as echarts from 'echarts'

let empGenderData = ref([])

// 存储图表实例
let myChart = null
let myChart2 = null
// 获取员工职位报表数据
async function selectEmpJobData() {
  const result = await selectEmpJobDataApi()
  if (result.code == 1) {
    let jobList = result.data.jobList
    let dataList = result.data.dataList

    initChart1(jobList, dataList)
  } else {
    ElMessage.error(result.message)
  }
}

// 获取员工性别报表数据
async function selectEmpGenderData() {
  const result = await selectEmpGenderDataApi()
  if (result.code == 1) {
    result.data.forEach(GenderData => {
      empGenderData.value.push(GenderData)
    });

    initChart2()
  } else {
    ElMessage.error(result.message)
  }
}



function initChart1(jobList, dataList) {

  // 初始化图表实例
  myChart = echarts.init(document.getElementById('container1'))

  // 图表配置项
  const option = {
    tooltip: {},
    xAxis: {
      type: 'category',
      data: jobList,
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: dataList,
        type: 'bar',
        itemStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 1,
            y2: 0,
            colorStops: [
              { offset: 0, color: '#fa709a' },   // 粉红色
              { offset: 1, color: '#fee140' }    // 浅黄色
            ]
          }
        }
      }
    ]
  }
  // 设置图表配置
  myChart.setOption(option)

  // 监听窗口大小变化，自动调整图表
  const handleResize = () => {
    myChart.resize()
  }
  window.addEventListener('resize', handleResize)

  // 组件卸载时移除事件监听
  onUnmounted(() => {
    window.removeEventListener('resize', handleResize)
  })
}

function initChart2() {
  // 初始化图表实例
  myChart2 = echarts.init(document.getElementById('container2'))

  // 图表配置项
  const option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      top: '5%',
      left: 'center'
    },
    series: [
      {
        type: 'pie',
        radius: ['40%', '70%'],
        avoidLabelOverlap: false,
        padAngle: 5,
        itemStyle: {
          borderRadius: 10
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 20,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: empGenderData.value
      }
    ]
  };
  // 设置图表配置
  myChart2.setOption(option)
}









// 在组件挂载后初始化图表
onMounted(() => {
  selectEmpGenderData()
  selectEmpJobData()
  if (myChart) {
    myChart.dispose()
    myChart = null
  }
  if (myChart2) {
    myChart2.dispose()
    myChart2 = null
  }
})
</script>

<style scoped>
.chart-container1 {
  width: 50%;
  height: 500px;
  /* 图表容器高度 */
}

.chart {
  width: 100%;
  height: 100%;
}

.chart-wrapper {
  display: flex;
  flex-direction: row;
  gap: 0px;
  /* 两个图表之间的间距 */
}
</style>
